package com.easy.user.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easy.common.bean.UserBean;
import com.easy.common.service.IUserService;
import com.easy.common.util.MonthGrowthVO;
import com.easy.common.util.Result;
import com.easy.user.service.TokenServiceImpl;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/user")
@RestController
@CrossOrigin
public class UserController {
    @Autowired
    IUserService userService;
    @Autowired
    TokenServiceImpl tokenService;
    //add注册添加用户
    @PostMapping("/add")
    public Result addUser(@RequestBody UserBean user){
        int result = userService.addUser(user);
        if(result>0){
            return Result.success("用户添加成功");
        }
        return Result.fail(400,"用户添加失败");
    }
    //管理员
    //all显示所有用户信息
    @GetMapping("/all")
    public Result getAll(UserBean user){
        Object user_data = userService.getAll(user);
        return Result.success(user_data);
    }
    //分页显示
    @GetMapping("/page")
    public Result getPage(UserBean user,Page<UserBean> page) {
        userService.getPage(user, page);
        return Result.success(page);
    }
    //edit修改用户信息
    @PutMapping("/edit")
    public Result uploadUser(@RequestBody UserBean user){
        int result = userService.uploadUser(user);
        if(result>0){
            return Result.success("用户信息修改成功");
        }
        return Result.fail(400,"用户信息修改失败");

    }
    //del封禁用户信息
    @PutMapping("/del/{id}")
    public Result reverseStatus(@PathVariable("id") Integer id){
        int result = userService.reverseStatus(id);
        if(result > 0){
            return Result.success("用户封禁成功");
        }
        return Result.fail(400,"用户封禁失败");
    }
    //用户
    //getid通过userid获取当前用户信息
    @GetMapping("/getid/{id}")
    public Result getUserById(@PathVariable("id") Integer id){
        UserBean user = userService.getUserById(id);
        if(user!=null){
            return Result.success(user);
        }
        return Result.fail(400,"用户不存在");
    }
    //delete注销用户信息
    @DeleteMapping("/delete/{id}")
    public Result deleteUser(@PathVariable("id") Integer id){
        int result = userService.deleteUser(id);
        if(result>0){
            return Result.success("用户删除成功");
        }
        return Result.fail(400,"用户删除失败");
    }

    /**
     * 获取月度用户增长统计
     */
    @GetMapping("/growth/monthly")
    public Result getMonthlyGrowth() {
        try {
            List<MonthGrowthVO> result = userService.getMonthlyUserGrowth();
            return Result.success(result);
        } catch (Exception e) {
            e.fillInStackTrace();
            return Result.fail(400,"获取数据失败");
        }
    }

//    // 修改密码
//    @PutMapping("/changePassword")
//    public Result changePassword(@RequestBody PasswordChangeRequest request, HttpServletRequest httpRequest) {
//        String token = httpRequest.getHeader("Authorization");
//        if (token != null && token.startsWith("Bearer ")) {
//            token = token.substring(7);
//        }
//
//        // 验证token
//        if (!tokenService.checkToken(token)) {
//            return Result.fail(400, "token无效或已过期");
//        }
//
//        int result = userService.changePassword(request, token);
//        if (result > 0) {
//            return Result.success("密码修改成功");
//        }
//        return Result.fail(400, "密码修改失败");
//    }

}